package com.maoxianzhe.gonghui.user.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.maoxianzhe.gonghui.common.Exception.BusinessException;
import com.maoxianzhe.gonghui.common.constant.BusinessConstant;
import com.maoxianzhe.gonghui.common.result.Result;
import com.maoxianzhe.gonghui.common.util.JwtUtil;
import com.maoxianzhe.gonghui.user.pojo.DTO.UserLoginDTO;
import com.maoxianzhe.gonghui.user.pojo.VO.UserLoginVO;
import com.maoxianzhe.gonghui.user.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @PostMapping("/login")
    public Result<UserLoginVO> UserLogin(@Valid @RequestBody UserLoginDTO userLoginDTO){
        if (userLoginDTO.getStudentId().isEmpty()){
            throw new BusinessException(BusinessConstant.STUDENT_ID_IS_NULL);
        }
        if ((userLoginDTO.getPassword()).isEmpty()){
            throw new BusinessException(BusinessConstant.PASSWORD_IS_NULL);
        }
        UserLoginVO userLoginVO = userService.userlogin(userLoginDTO);
        JwtUtil.createToken(userLoginVO.getId(),userLoginVO.getStudentId());
        return Result.success(userLoginVO);
    }
}
